package com.practice.mybpapi.controller;

import cn.hutool.core.bean.BeanUtil;
import com.practice.mybpapi.domain.dto.*;
import com.practice.mybpapi.domain.po.User;
import com.practice.mybpapi.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "用户服务")
@RestController
@RequestMapping("/users")
public class UserController {

    private final UserService userService;
    
    public UserController(UserService userService) {
        this.userService = userService;
    }
    
    @ApiOperation("查询用户")
    @RequestMapping("/query")
    public String query() {
        return "query user";
    }

    // 新增用户
    @ApiOperation("新增用户")
    @PostMapping("/")
    public String save(@RequestBody CreateUserDto userDto) {
        User user = BeanUtil.copyProperties(userDto, User.class);
        userService.save(user);
        return "save user success";
    }

    // 删除用户
    @ApiOperation("删除用户")
    @DeleteMapping("/users/{id}")
    public String delete(@PathVariable Long id) {
        userService.removeById(id);
        return "delete user success";
    }

    // 根据id查询用户
    @ApiOperation("根据id查询用户")
    @GetMapping("/users/{id}")
    public QueryUserVo queryById(@PathVariable Long id) {
        QueryUserVo user = userService.getUserAddress(id);
        return user;
    }

    // 根据id批量查询用户
    @ApiOperation("根据id批量查询用户")
    @GetMapping("/users")
    public List<QueryUserVo> queryByIds(@RequestParam List<Long> ids) {
        return userService.batchGetUserAddress(ids);
        /*List<QueryUserVo> users = userService.batchGetUserAddress(ids);
        return BeanUtil.copyToList(users, QueryUserVo.class);*/
    }

    // 根据id扣减余额
    @ApiOperation("根据id扣减余额")
    @PutMapping("/users/{id}/decrease/{money}")
    public String decrease(@PathVariable Long id, @PathVariable Integer money) {
        userService.decrease(id, money);
        return "decrease user balance success";
    }

    @ApiOperation("根据条件查询用户")
    @GetMapping("/list")
    public List<QueryUserVo> query(UserQuery query) {
        return userService.queryUser(query);
    }

    @ApiOperation("分页查询用户")
    @GetMapping("/page")
    public PageBase<QueryUserVo> pageQuery(PageUserQuery query) {
        return userService.pagedQueryUser(query);
    }
}